﻿namespace Blaze.Framework.Loaders
{
    using Logging;
    using UnityEngine;
    using UnityEngine.Networking;

    public sealed class UnityWebRequestTextureLoader : UnityWebRequestLoader
    {
        public bool IsReadable { get; private set; }

        public Texture2D Texture2D => ((DownloadHandlerTexture) UnityWebRequest?.downloadHandler)?.texture;

        /// <summary>
        /// 设置一个值，表示是否启用读写加载出来的贴图。
        /// </summary>
        public UnityWebRequestTextureLoader SetReadable(bool isReadable)
        {
            IsReadable = isReadable;
            return this;
        }

        protected override UnityWebRequest GetRequest()
        {
            var url = GetUrl();
            var ret = UnityWebRequestTexture.GetTexture(url, !IsReadable);
            return ret;
        }

        private readonly Log mLog = LogManager.GetLogger(typeof(UnityWebRequestTextureLoader));
    }
}